package com.ruoyi.largeScreen.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.largeScreen.domain.ZjcEchartsStore;
import com.ruoyi.largeScreen.service.IZjcEchartsStoreService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * echarts仓库Controller
 *
 * @author ruoyi
 * @date 2025-04-19
 */
@RestController
@RequestMapping("/largeScreen/echartsStore")
public class ZjcEchartsStoreController extends BaseController {
    @Autowired
    private IZjcEchartsStoreService zjcEchartsStoreService;

    @PostMapping("/initData")
    public AjaxResult InitData(@RequestBody String dataStr) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(dataStr);
        JsonNode dataStrNode = rootNode.get("dataStr");
        String jsonData = dataStrNode.asText();
        JavaType listType = objectMapper.getTypeFactory().constructCollectionType(List.class, ZjcEchartsStore.class);
        List<ZjcEchartsStore> list = objectMapper.readValue(jsonData, listType);

        for(int i=0;i<list.size();i++){
            zjcEchartsStoreService.insertZjcEchartsStore(list.get(i));
        }

        return success(list);
    }

    /**
     * 查询echarts仓库列表
     */
//    @PreAuthorize("@ss.hasPermi('largeScreen:echartsStore:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZjcEchartsStore zjcEchartsStore) {
        startPage();
        List<ZjcEchartsStore> list = zjcEchartsStoreService.selectZjcEchartsStoreList(zjcEchartsStore);
        return getDataTable(list);
    }

    /**
     * 导出echarts仓库列表
     */
    @PreAuthorize("@ss.hasPermi('largeScreen:echartsStore:export')")
    @Log(title = "echarts仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZjcEchartsStore zjcEchartsStore) {
        List<ZjcEchartsStore> list = zjcEchartsStoreService.selectZjcEchartsStoreList(zjcEchartsStore);
        ExcelUtil<ZjcEchartsStore> util = new ExcelUtil<ZjcEchartsStore>(ZjcEchartsStore.class);
        util.exportExcel(response, list, "echarts仓库数据");
    }

    /**
     * 获取echarts仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('largeScreen:echartsStore:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zjcEchartsStoreService.selectZjcEchartsStoreById(id));
    }

    /**
     * 新增echarts仓库
     */
    @PreAuthorize("@ss.hasPermi('largeScreen:echartsStore:add')")
    @Log(title = "echarts仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZjcEchartsStore zjcEchartsStore) {
        return toAjax(zjcEchartsStoreService.insertZjcEchartsStore(zjcEchartsStore));
    }

    /**
     * 修改echarts仓库
     */
    @PreAuthorize("@ss.hasPermi('largeScreen:echartsStore:edit')")
    @Log(title = "echarts仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZjcEchartsStore zjcEchartsStore) {
        return toAjax(zjcEchartsStoreService.updateZjcEchartsStore(zjcEchartsStore));
    }

    /**
     * 删除echarts仓库
     */
    @PreAuthorize("@ss.hasPermi('largeScreen:echartsStore:remove')")
    @Log(title = "echarts仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(zjcEchartsStoreService.deleteZjcEchartsStoreByIds(ids));
    }
}
